package top.drinyeit.nongmao.presenter;

import android.app.Activity;
import android.content.Intent;

import com.xgr.easypay.EasyPay;
import com.xgr.easypay.alipay.AliPay;
import com.xgr.easypay.alipay.AlipayInfoImpli;
import com.xgr.easypay.callback.IPayCallback;
import com.xgr.easypay.wxpay.WXPay;
import com.xgr.easypay.wxpay.WXPayInfoImpli;

import java.math.BigDecimal;

import top.drinyeit.nongmao.activity.MainActivity;
import top.drinyeit.nongmao.activity.MyOrderActivity;
import top.drinyeit.nongmao.dialog.PayDialog;
import top.drinyeit.nongmao.model.WxBean;
import top.drinyeit.nongmao.retrofit.HttpResponse;
import top.drinyeit.nongmao.retrofit.RetrofitMethod;
import top.drinyeit.nongmao.retrofit.subscriber.CommonSubscriber;
import top.drinyeit.nongmao.retrofit.subscriber.SubscriberListener;

import static top.drinyeit.nongmao.app.Constant.COMMON_SUCCESS;

/**
 * Created by wu on 2018/1/16.
 */

public class PayDialogPresenter {
    private PayDialog dialog;

    public PayDialogPresenter(PayDialog dialog) {
        this.dialog = dialog;
    }

    //支付宝支付
    public void zfbPayment(BigDecimal order_price, String order_id) {
        CommonSubscriber<HttpResponse<String>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<String>>() {
            @Override
            public void onError(String e, int code) {
                dialog.showToastMsg(e);
            }

            @Override
            public void onNext(HttpResponse<String> o) {
                dialog.dismissLoading();
                if (o.getCode() == COMMON_SUCCESS) {
                    alipay(o.getData());
                }
            }
        });
        RetrofitMethod.getInstance().zfbPayment(subscriber, order_price, order_id);
    }

    //微信支付
    public void vxPayment(BigDecimal order_price, String order_id) {
        CommonSubscriber<HttpResponse<WxBean>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<WxBean>>() {
            @Override
            public void onError(String e, int code) {
                dialog.showToastMsg(e);
            }

            @Override
            public void onNext(HttpResponse<WxBean> o) {
                dialog.dismissLoading();
                if (o.getCode() == COMMON_SUCCESS) {
                    //实例化微信支付策略
                    String wxAppId = "wxf6a615b95eb55ce5";
                    WXPay wxPay = WXPay.getInstance( dialog.activity, wxAppId);
                    //构造微信订单实体。一般都是由服务端直接返回。
                    WXPayInfoImpli wxPayInfoImpli = new WXPayInfoImpli();
                    wxPayInfoImpli.setTimestamp(o.getData().getTimestamp());
                    wxPayInfoImpli.setSign(o.getData().getSign());
                    wxPayInfoImpli.setPrepayId(o.getData().getPrepayid());
                    wxPayInfoImpli.setPartnerid(o.getData().getPartnerid());
                    wxPayInfoImpli.setAppid(o.getData().getAppid());
                    wxPayInfoImpli.setNonceStr(o.getData().getNoncestr());
                    wxPayInfoImpli.setPackageValue("Sign=WXPay");
                    //策略场景类调起支付方法开始支付，以及接收回调。
                    EasyPay.pay(wxPay, (Activity) dialog.activity, wxPayInfoImpli, new IPayCallback() {
                        @Override
                        public void success() {
                            dialog.dismiss();
                            dialog.showToastMsg("支付成功");
                            Intent intent0 = new Intent(dialog.getContext(), MainActivity.class);
                            intent0.putExtra("position",3);
                            Intent intent1 = new Intent(dialog.getContext(),MyOrderActivity.class);
                            intent1.putExtra("position",1);
                            Intent[] intents = new Intent[]{intent0,intent1};
                            dialog.getContext().startActivities(intents);
                        }

                        @Override
                        public void failed() {
                            dialog.showToastMsg("支付失败");
                        }

                        @Override
                        public void cancel() {
                            dialog.showToastMsg("取消支付");
                        }
                    });

                }
            }
        });
        RetrofitMethod.getInstance().vxPayment(subscriber, order_price, order_id);
    }

    //余额支付
    public void balancePayment(BigDecimal order_price, String order_id) {
        CommonSubscriber<HttpResponse<String>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<String>>() {
            @Override
            public void onError(String e, int code) {
                dialog.showToastMsg(e);
            }

            @Override
            public void onNext(HttpResponse<String> o) {
                dialog.dismissLoading();
                dialog.showToastMsg(o.getMessage());
                if (o.getCode() == COMMON_SUCCESS) {
                    dialog.dismiss();
                    Intent intent0 = new Intent(dialog.getContext(), MainActivity.class);
                    intent0.putExtra("position",3);
                    Intent intent1 = new Intent(dialog.getContext(),MyOrderActivity.class);
                    intent1.putExtra("position",1);
                    Intent[] intents = new Intent[]{intent0,intent1};
                    dialog.getContext().startActivities(intents);
                }
            }
        });
        RetrofitMethod.getInstance().balancePayment(subscriber, order_price, order_id);
    }

    private void alipay(String a) {
        AliPay aliPay = new AliPay();
        AlipayInfoImpli alipayInfoImpli = new AlipayInfoImpli();
        alipayInfoImpli.setOrderInfo(a);
        EasyPay.pay(aliPay, (Activity) dialog.activity, alipayInfoImpli, new IPayCallback() {
            @Override
            public void success() {
                dialog.dismiss();
                dialog.showToastMsg("支付成功");
                Intent intent0 = new Intent(dialog.activity, MainActivity.class);
                intent0.putExtra("position",3);
                Intent intent1 = new Intent(dialog.activity,MyOrderActivity.class);
                intent1.putExtra("position",1);
                Intent[] intents = new Intent[]{intent0,intent1};
                dialog.getContext().startActivities(intents);
            }

            @Override
            public void failed() {
                dialog.showToastMsg("支付失败");
            }

            @Override
            public void cancel() {
                dialog.showToastMsg("支付取消");
            }
        });
    }
}
